package edu.gdkm.controller;

import edu.gdkm.common.Result;
import edu.gdkm.controller.request.BookPageRequest;
import edu.gdkm.controller.request.CategoryPageRequest;
import edu.gdkm.entity.Book;
import edu.gdkm.service.IBookService;
import edu.gdkm.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//解决跨域问题
@CrossOrigin
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    IBookService bookService;



    /**
     * 修改信息
     * @param obj
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Book obj){
        bookService.update(obj);
        return Result.success();
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        bookService.deleteById(id);
        return Result.success();
    }



    /**
     * 通过id获取用户数据 用于=>(修改,删除用户)
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Book obj = bookService.getById(id);
        return Result.success(obj);
    }
    /**
     * 新增用户
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Book obj){

        bookService.save(obj);
        return Result.success();
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        List<Book> list = bookService.list();
        return Result.success(list);
    }

    /**
     * 分页查询 模糊查询
     * @param
     * @return
     */
    @GetMapping("/page")
    public Result page(BookPageRequest pageRequest){
        return Result.success(bookService.page(pageRequest));
    }


}
